 
package com.zdkj.subway.rca.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.zdkj.common.core.exceltools.MultipartFileToFile;
import com.zdkj.common.security.service.PigxUser;
import com.zdkj.common.security.util.SecurityUtils;

import com.zdkj.subway.rca.entity.SwCkDistinctPayment;
import com.zdkj.subway.rca.entity.SwCkDistinctPaymentFaceTmp;
import com.zdkj.subway.rca.entity.SwCkDistinctPaymentConnectivityTmp;
import com.zdkj.subway.rca.entity.SwCkDistinctPaymentQrTmp;
import com.zdkj.subway.rca.entity.SwCkDistinctPaymentTvmTmp;
import com.zdkj.subway.rca.mapper.SwCkDistinctPaymentMapper;

import com.zdkj.subway.rca.service.SwCkDistinctPaymentFaceTmpService;
import com.zdkj.subway.rca.service.SwCkDistinctPaymentConnectivityTmpService;
import com.zdkj.subway.rca.service.SwCkDistinctPaymentQrTmpService;
import com.zdkj.subway.rca.service.SwCkDistinctPaymentService;
import com.zdkj.subway.rca.service.SwCkDistinctPaymentTvmTmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * VIEW
 *
 * @author stw
 * @date 2020-10-29 10:35:47
 */
@Service
public class SwCkDistinctPaymentServiceImpl extends ServiceImpl<SwCkDistinctPaymentMapper, SwCkDistinctPayment> implements SwCkDistinctPaymentService {

    @Autowired
    private SwCkDistinctPaymentQrTmpService swCkDistinctPaymentQrTmpService;

    @Autowired
    private SwCkDistinctPaymentTvmTmpService swCkDistinctPaymentTvmTmpService;

    @Autowired
    private SwCkDistinctPaymentFaceTmpService swCkDistinctPaymentFaceTmpService;

	@Autowired
    private SwCkDistinctPaymentConnectivityTmpService swCkDistinctPaymentConnectivityTmpService;

    @Override
    public void exportTamplates(String verfiyConfirmFlag, HttpServletResponse response) {
        String verifyConfirmName = null;
        String tableName = null;
        if ("1".equals(verfiyConfirmFlag)) {
            verifyConfirmName = "微信支付宝银联";
            tableName = "sw_ck_distinct_payment_qr";
        } else if("2".equals(verfiyConfirmFlag)) {
            verifyConfirmName = "TVM";
            tableName = "sw_ck_distinct_payment_tvm";
        } else if("3".equals(verfiyConfirmFlag)) {
            verifyConfirmName = "人脸";
            tableName = "sw_ck_distinct_payment_face";
        }else if("4".equals(verfiyConfirmFlag)) {
            verifyConfirmName = "应急及互联互通";
            tableName = "sw_ck_distinct_payment_connectivity";
        }

        BufferedInputStream br = null;
        OutputStream out = null;
        try {
            response.reset(); // 非常重要
            response.setContentType("application/vnd.ms-excel");
            response.setCharacterEncoding("utf-8");
            final String fileName = URLEncoder.encode(verifyConfirmName, "UTF-8");
            response.setHeader("Content-disposition", "attachment;filename=" + fileName + System.currentTimeMillis() + ".xlsx");

            br =  new BufferedInputStream(this.getClass().getResourceAsStream("/static/excel/template/import/" + tableName + ".xlsx"));

            byte[] buf = new byte[1024];
            int len = 0;

            out = response.getOutputStream();
            while ((len = br.read(buf)) > 0) {
                out.write(buf, 0, len);
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if(out != null) {
                try {
                    out.close();
                } catch (IOException ioe) {
                    ioe.printStackTrace();
                }
            }
            if(br != null){
                try {
                    br.close();
                } catch (IOException ioe) {
                    ioe.printStackTrace();
                }
            }
        }
    }

    @Override
    public Boolean saveBatchWithExcel(MultipartFile file, String verfiyConfirmFlag) throws Exception {

        File excelFile = MultipartFileToFile.multipartFileToFile(file);
        if("1".equals(verfiyConfirmFlag)) {
            swCkDistinctPaymentQrTmpService.saveBatchWithExcel(excelFile);
        } else if("2".equals(verfiyConfirmFlag)) {
            swCkDistinctPaymentTvmTmpService.saveBatchWithExcel(excelFile);
        }  else if("3".equals(verfiyConfirmFlag)) {
            swCkDistinctPaymentFaceTmpService.saveBatchWithExcel(excelFile);
        } else if("4".equals(verfiyConfirmFlag)) {
            swCkDistinctPaymentConnectivityTmpService.saveBatchWithExcel(excelFile);
        }

        return true;
    }

    @Override
    public List<Map<String,String>> getSwCkDistinctPaymentConfirmTableList() {

        List<Map<String,String>> listMap = new ArrayList<>();
        Map<String,String> mapQr = new HashMap<>();
        mapQr.put("verfiyConfirmFlag","1");
        mapQr.put("verifyConfirmName","微信支付宝银联");
        SwCkDistinctPaymentQrTmp swCkDistinctPaymentQrTmp = new SwCkDistinctPaymentQrTmp();
        PigxUser user = SecurityUtils.getUser();
        swCkDistinctPaymentQrTmp.setCreatorId(user.getId());
        if(swCkDistinctPaymentQrTmpService.count(Wrappers.query(swCkDistinctPaymentQrTmp)) > 0 ){
            mapQr.put("importStatus","未确认");
        } else {
            mapQr.put("importStatus","已确认或已取消");
        }
        listMap.add(mapQr);

        Map<String,String> mapTvm = new HashMap<>();
        mapTvm.put("verfiyConfirmFlag","2");
        mapTvm.put("verifyConfirmName","TVM电子支付平台");
        SwCkDistinctPaymentTvmTmp swCkDistinctPaymentTvmTmp = new SwCkDistinctPaymentTvmTmp();

        swCkDistinctPaymentTvmTmp.setCreatorId(user.getId());
        if(swCkDistinctPaymentTvmTmpService.count(Wrappers.query(swCkDistinctPaymentTvmTmp)) > 0 ){
            mapTvm.put("importStatus","未确认");
        } else {
            mapTvm.put("importStatus","已确认或已取消");
        }
        listMap.add(mapTvm);

        Map<String,String> mapFace = new HashMap<>();
        mapFace.put("verfiyConfirmFlag","3");
        mapFace.put("verifyConfirmName","人脸");
        SwCkDistinctPaymentFaceTmp swCkDistinctPaymentFaceTmp = new SwCkDistinctPaymentFaceTmp();

        swCkDistinctPaymentFaceTmp.setCreatorId(user.getId());
        if(swCkDistinctPaymentFaceTmpService.count(Wrappers.query(swCkDistinctPaymentFaceTmp)) > 0 ){
            mapFace.put("importStatus","未确认");
        } else {
            mapFace.put("importStatus","已确认或已取消");
        }
        listMap.add(mapFace);

		Map<String,String> mapConnectivity = new HashMap<>();
        mapConnectivity.put("verfiyConfirmFlag","4");
        mapConnectivity.put("verifyConfirmName","应急及互联互通");
        SwCkDistinctPaymentConnectivityTmp swCkDistinctPaymentConnectivityTmp = new SwCkDistinctPaymentConnectivityTmp();

        swCkDistinctPaymentConnectivityTmp.setCreatorId(user.getId());
        if(swCkDistinctPaymentConnectivityTmpService.count(Wrappers.query(swCkDistinctPaymentConnectivityTmp)) > 0 ){
            mapConnectivity.put("importStatus","未确认");
        } else {
            mapConnectivity.put("importStatus","已确认或已取消");
        }
        listMap.add(mapConnectivity);

        return listMap;
    }
    @Override
    public List<Map<String,String>> selectLineMap() {
        return this.baseMapper.selectLineMap();
    }
    @Override
    public List<Map<String,String>> selectStationMap() {
        return this.baseMapper.selectStationMap();
    }
}
